/* 计算器（一户有一台就够） */
class Calculator {
    add(a: number, b: number) {
        return a + b
    }
    multiply(a: number, b: number) {
        return a * b
    }

    /* 确保计算器有且只有一台 singleton=单例/独子 */
    static singleton: Calculator | null = null

    // Calculator.getSingleton()
    static getSingleton() {
        if (!this.singleton) {
            this.singleton = new Calculator()
        }
        return this.singleton
    }
}

/* 很淡腾地创建了无数相同功能的实例 作者死于内存爆炸 */
// const calc1 = new Calculator()
// console.log(calc1.add(2, 3));
// const calc2 = new Calculator()
// console.log(calc2.multiply(2, 3));
// const calc3 = new Calculator()
// console.log(calc3.multiply(5, 6));

const calc1 = Calculator.getSingleton()
console.log(calc1.add(2, 3));

const calc2 = Calculator.getSingleton()
console.log(calc2.add(2, 3));

const calc3 = Calculator.getSingleton()
console.log(calc3.add(2, 3));

console.log(calc2 === calc3);//

